define(["jquery","bbcare","echarts","jqprint","as/app/dst/dstHistoryRecord.servers","as/app/dst/dstHistoryRecord.ui"],
    function($,$bb,echarts,$jqprint,$servers,$ui){
	var event = {};
    var doctor = JSON.parse($bb.getSessionStorage("account")).alias;

    // 打印
    var onPrintClick = function(){
        $("#printContent").jqprint({
            debug: false, //如果是true则可以显示iframe查看效果（iframe默认高和宽都很小，可以再源码中调大），默认是false
            importCSS: true, //true表示引进原来的页面的css，默认是true。（如果是true，先会找$("link[media=print]")，若没有会去找$("link")中的css文件）
            printContainer: true, //表示如果原来选择的对象必须被纳入打印（注意：设置为false可能会打破你的CSS规则）。
            operaSupport: false//表示如果插件也必须支持歌opera浏览器，在这种情况下，它提供了建立一个临时的打印选项卡。默认是true
        });
        return;
    };

    /**
    * 图表
    */
    var charts = function(obj,domId){
        var handleObj = $servers.handleCharts(obj.rows);
        var myChart = echarts.init(document.getElementById(domId));
        var option = {
            title: {
                text: 'DST发育趋势',
                left: "center",
                textStyle:{
                    color: "#333",
                }
            },
            tooltip: {
                trigger: 'axis',
                formatter:function(params, ticket, callback){
                    var val='';
                    if( params && $.isArray(params) ){
                        for (var i = 0; i < params.length; i++) {
                            params[i].value = params[i].value < 50 ? "低于50" : params[i].value > 150 ? "高于150" : params[i].value;
                            val += params[i].seriesName == "DQ" ? "DQ："+params[i].value + "<br/>" : "MI："+params[i].value;
                        }
                    }
                    return val;
                }
            },
            legend: {
                right:"20",
                data:['DQ','MI']
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: handleObj.arrTime
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name:'DQ',
                    type:'line',  
                    smooth:true,    //曲线变平滑 
                    data:handleObj.arrDq
                },
                {
                    name:'MI',
                    type:'line',
                    smooth:true,    //曲线变平滑 
                    data:handleObj.arrMi
                },
            ]
        };
        myChart.setOption(option);
        $("#resultLineChart-print").html('<img src="' + myChart.getDataURL({type: "png",backgroundColor: '#fff'}) + '" style="width: 100%;height:100%; "/>');
    };

    /**
     * 调整大小
     */
    var resize = function(dom){
        $(window).resize(function(){
            var width = $(dom).width();
            $(dom + ">div").css({"width":width});
            $(dom + ">div>canvas").css({"width":width});
        });
    }

    /**
     * 查询评估用户信息成功后的回调函数
     */
    var ajaxRecordUserlistCallBack = function(data){
        charts(data,"resultLineChart");
        resize("#resultLineChart");
        // $(".dstDetail-wrap").parents(".assess-wrap").length 判断是从患者中心打开 还是从dst菜单打开
        if( $(".dstHistoryRecord-wrap").parents(".assess-wrap").length == 0 ){
            // 打印
            var topbars = [
                {
                    "icon":"fa fa-print",
                    "name":"打印报告",
                    "callback":onPrintClick
                }
            ];
            $bb.toolbar(topbars);
        }
    }

    /**
     * 环形
     */
    var cirleChart = function(domId,data){
        var myChart = echarts.init(document.getElementById(domId));
        var option = {
            color:['#FF795A','#A8A8A8','#FFB14A','#DDDDDD'],
            series: [
                {
                    name:'',
                    type:'pie',
                    radius: ['70%', '80%'],
                    hoverAnimation:false,
                    animation: false,
                    label: {
                        normal: {
                            show: false,
                        }
                    },
                    data:[
                        {value:data.dqScore, name:"发育商"},
                        {value:150-data.dqScore, name:"发育商"}
                    ]
                },
                {
                    name:'',
                    type:'pie',
                    radius: ['81%', '91%'],
                    hoverAnimation:false,
                    animation: false,
                    label: {
                        normal: {
                            show: false,
                        }
                    },
                    data:[
                        {
                            value:data.miScore, 
                            name:"智力指数",
                            itemStyle:{
                                borderColor: "blue",
                            },
                            label: {
                                normal: {
                                    show: true,
                                    position: 'center',
                                    formatter:'测验结果 \n',
                                    textStyle:{
                                        fontSize: 14,
                                        fontWeight:"bold",
                                        color:'#333'
                                    }
                                }
                            }
                        },
                        {
                            value:150-data.miScore, 
                            name:"智力指数",
                            label: {
                                normal: {
                                    show: true,
                                    position: 'center',
                                    formatter:data.level,
                                    textStyle:{
                                        fontSize: 20,
                                        fontWeight:"bolder",
                                        color: '#FF795A'
                                    }
                                }
                            }
                        }
                    ]
                }
            ]
        };
        myChart.setOption(option);
        $("#" + domId + "-print").html('<img src="' + myChart.getDataURL({type: "png",backgroundColor: '#fff'}) + '" style="width: 100%;height:100%; "/>');
    }

    /**
     * 请求单个评估详情成功后的回调函数
     */
    var ajaxEvaluationDetailCallBack = function(data){
        data = $servers.handleInfo(data);
        $ui.info({
            "id":"basicInfo",
            "info":data
        });
        $ui.info({
            "id":"basicInfo-print",
            "info":data
        });
        data = $servers.handleRecord(data);
        $ui.result({
            "id":"dstResult",
            "info":data
        });
        $ui.resultPrint({
            "id":"dstResult-print",
            "info":data
        });
        cirleChart("cirleChart",data);
        $("#doctor").html(data.doctorName);
        $("#testTime").html(data.testTime);
        $servers.ajaxRecordUserlist(data,ajaxRecordUserlistCallBack);
    }

	/**
	 * 点击评估历史，进行切换
	 */
	event.onHistorySelect = function(){
        $(this).addClass("active").siblings().removeClass("active");
        var recordId = $(this).attr("data-id");
        $servers.ajaxEvaluationDetail(recordId,ajaxEvaluationDetailCallBack);
	}

	return event;
})